//
//  CZMainController.swift
//  weibo
//
//  Created by Mac on 16/5/10.
//  Copyright © 2016年 fengzhixiang. All rights reserved.
//

import UIKit

class CZMainController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        //1.定义闭包
        let composeCallback = { () -> () in
            //Modal出控制器
            print("Modal出控制器")
        }

        // get,只读的属性,没有set方法, = 赋值调用的是set放法,只读属性没有了set就不能用=直接赋值
        // 使用KVC,KVC会先去找set方法如果没有set方法会找_tabBar成员变量,KVC使用运行时
        let mainTabBar = CZMainTabBar()

        //2.传递闭包()
        mainTabBar.composeCallback = composeCallback

        //修改系统tabBar为CZMainTabBar,会自动调用方法
        setValue(mainTabBar, forKey: "tabBar")
        setupChilderViewController()
    }

    /**
     抽取设置tabbar子控制器的方法
     */
    func setupChilderViewController(){

        //首页
        let homeVC = CZHomeViewController()
        addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")

        //消息
        let messageVC = CZMessageViewController()
        addChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")

        //发现
        let disoverVC = CZDisoverViewController()
        addChildViewController(disoverVC, title: "发现", imageName: "tabbar_discover")

        //我
        let profileVC = CZProfileViewController()
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")

    }

    /// 添加子控制器,设置标题和图片名称,并且包装导航控制器
    func addChildViewController(childController: UIViewController, title:String, imageName:String) {
        //设置标题
        childController.title = title
        //设置tabbar图片
        childController.tabBarItem.image = UIImage(named:imageName)
        //设置高亮图片
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置文字选择颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //包装导航控制器
        self.addChildViewController(UINavigationController(rootViewController: childController))
    }


}
